package autoorder.zsxmlv.service;

import autoorder.zsxmlv.enums.TaskEnum;
import autoorder.zsxmlv.mapper.TaskMapper;
import autoorder.zsxmlv.po.Task;
import common.zsxmlv.enums.ResultEnum;
import common.zsxmlv.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: ZSXM
 * @Description: 爬虫任务相关
 * @Company: DingZheng
 * @Date: Create by 2018/6/8 0:06
 * @Version: 1.0
 * @Modify by:
 */
@Service
public class TaskService {

    @Resource
    private TaskMapper taskMapper;


    /**
     * 创建新的任务
     * @return
     */
    public Result insert() {
        int insert = taskMapper.insert(new Task());
        Result result = this.getResult(insert);
        return result;
    }

    /**
     * 获取当前任务状态
     * @return 返回相关任务状态的result
     */
    public Result getTaskStatus() {
        Result result = new Result();
        Task byMaxId = taskMapper.selectByMaxId();
        if (byMaxId == null) {
            result.setMsg(TaskEnum.TASK_NOT_CREATE.getMsg());
            result.setCode(TaskEnum.TASK_NOT_CREATE.getCode());
        } else {
            Integer executeStatus = byMaxId.getExecuteStatus();
            if (TaskEnum.TASK_EXECUTING.getCode().equals(executeStatus)) {
                result.setCode(TaskEnum.TASK_EXECUTING.getCode());
                result.setMsg(TaskEnum.TASK_EXECUTING.getMsg());
            } else if (TaskEnum.TASK_FINISH.getCode().equals(executeStatus)) {
                result.setCode(TaskEnum.TASK_FINISH.getCode());
                result.setMsg(TaskEnum.TASK_FINISH.getMsg());
            } else {
                result.setMsg(TaskEnum.TASK_UNKNOWN_STATUS.getMsg());
                result.setCode(TaskEnum.TASK_UNKNOWN_STATUS.getCode());
            }
        }
        return result;
    }

    /**
     * 获取当前执行的任务
     * @return
     */
    public Task selectByMaxId() {
        return taskMapper.selectByMaxId();
    }


    /**
     * 获取返回结果
     * @param insert
     * @return
     */
    private Result getResult(Integer insert) {
        Result result = new Result();
        if (insert > 0) {
            result.setSuccess(true);
            result.setCode(TaskEnum.TASK_CREATE_SUCCESS.getCode());
            result.setMsg(TaskEnum.TASK_CREATE_SUCCESS.getMsg());
        } else {
            result.setSuccess(false);
            result.setCode(TaskEnum.TASK_CREATE_FAIL.getCode());
            result.setMsg(TaskEnum.TASK_CREATE_FAIL.getMsg());
        }
        return result;
    }

    /**
     * 更新任务
     * @param task
     * @return
     */
    public Result updateTask(Task task) {
        Result result = new Result();
        if (task != null) {
            int i = taskMapper.updateByPrimaryKey(task);
            result.setCode(TaskEnum.TASK_UPDATE_SUCCESS.getCode());
            result.setMsg(TaskEnum.TASK_UPDATE_SUCCESS.getMsg());
        } else {
            result.setCode(TaskEnum.TASK_UPDATE_ERROR.getCode());
            result.setMsg(TaskEnum.TASK_UPDATE_ERROR.getMsg());
        }
        return result;
    }

}
